package cn.school.controller.test;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * 文件工具类
 */
@Slf4j
public class FilePathUtils {
    public static String getUploadDir(String dirPath){
        //上传目录地址
        String uploadDir = dirPath;

        String[] split = dirPath.split("/");
        StringBuffer addurl = new StringBuffer();
        for(String url:split){
            addurl.append(url+"/");
            File dir = new File(addurl.toString());
            if(!dir.exists())
            {
                boolean mkdir = dir.mkdir();
                if(Boolean.FALSE.equals(mkdir)){
                    log.warn("创建目录{}失败",uploadDir);
                    return null;
                }
            }
        }
        //如果目录不存在，自动创建文件夹

        return uploadDir;
    }

   public static String getUrlPathByFileName(HttpServletRequest request, String fileName){
       return request.getScheme()+"://"+request.getServerName()+":"+
               request.getServerPort()+"/img/"+fileName;
    }
}
